AmiBroker Formula Language (AFL)

(Revision 1.1. - Feb 28th, 1999)

Note: AmiBroker 3.0 fully supports AFL 1.1 specification. Previous specifiaction (AFL 1.0) applies to AmiBroker 2.9beta.

Introduction

AFL is a special programming language used to define and create custom indicators, analyses and simulation tests and guru commentaries. Unfortunatelly it is not compatible with previous Rule Definition Language used by pre-2.9 releases of AmiBroker. However it is very easy to port all formulas to a new language. On the other hand AFL is similiar to very well known Metastock formula language. AFL is comprised of high-level functions (e.g., ma(), rsi(), abs() ), mathematical operators (e.g., +, -, /, *), and parameters (open, high, low, close, etc.). Each of these basic components can be combined in a unique way to create your own indicators or trading rules or Guru comments. AFL is the foundation for all AmiBroker analysis tools.

Basics

Each formula in AFL contains of one or more statements (lines). Each statement MUST be terminated by semicolon (;). In this way you are able to break long expressions into several physical lines (in order to gain clarity) and AmiBroker will still treat it like a single statement until terminating semicolon. Example (single variable assignment statement):

my_indicator = iif( macd() > 0,
            close - ma(close,9),
            ma( close, 9 ) - close ); 

One of the most important building blocks of a formula is called a price array identifier. It identifies specific price fields that the formula should operate on. The valid price array identifiers are open, high, low, close, volume, sales, average. Price array identifiers can be abbreviated as shown in the following table. Note that these are not case-specific.

Long name Abbreviation Comment
Open O  
High H  
Low L  
Close C  
Volume V  
Sales S  
Average A (H+L+C)/3

Examples of the use of price array identifiers in formulas are shown below.

ma( close, 10 ); iif( h > ref(h,-1), ma(h,20), ma(c,20) ); 

Operators

Mathematical operators are the "glue" that binds formulas. Formulas can contain the following mathematical operators:

The following formulas illustrate the use of operators in a formula:

( H + L ) / 2;
ma(c,10)-ma(c,20) / (h + l + c);
close + ((1.02 * high)-high); 

Parentheses

As you probably noticed AFL supports parentheses in formulas.

Parentheses can be used to control the operation precedence (the order in which the operators are calculated). AmiBroker always does operations within the innermost parentheses first. When parentheses are not used, the precedence is as follows (higher precedence listed first):

  1. ^ Exponentiation
  2. - Negation - Unary minus
  3. * Multiplication
  4. / Division
  5. + Addition
  6. - Subtraction
  7. < Less than
  8. > Greater than
  9. <= Less than or equal to
  10. >= Greater than or equal to
  11. = = Equal to
  12. != Not equal to
  13. NOT
  14. Logical "Not"
  15. AND Logical "And"
  16. OR Logical "Or"
  17. = Variable assignment operator

The expression

H + L / 2; 

(without parenthesis) would be calculated by AmiBroker as "L / 2" plus "H", since division has a higher precedence. This would result in a much different value than

(H + L)/2; 

Functions

In addition to mathematical operators, AmiBroker contains over 50 functions that perform mathematical operations.

The following formula consists of a single function that plots the square roots of the closing prices:

sqrt( CLOSE ); 

The following formula consists of a single function that plots a 14-period RSI indicator:

rsi(14); 

The following formula consists of two functions. The result is the difference between the MACD indicator and a 9-period exponential moving average of the MACD:

macd()- ema(macd(),9); 

All functions must be followed by a pair of parentheses.

As has been eluded to in earlier examples, a function can be "nested" within a function. The nested function can serve as the main function's data array parameter. The following examples show functions nested within functions:

ma( rsi(15), 10 ); 
ma( ema( rsi(15), 20), 10 ); 

The first example calculates a 10-period simple moving average of a 15-period Relative Strength Index (RSI). The second example calculates a 20-period exponential moving average of a 15-period RSI, and then calculates a 10-period simple moving average of this moving average.

Conditional function IIF()

The iif() function is used to create conditional (i.e., "if-then") statements. It contains three parameters as shown in the following example.

iif( close > ma(c,10), rsi(9), rsi(14) ); 

The above "iif" statement reads (in English) as follows: If today's close is greater than today's 10-day simple moving average of the close, then plot a 9-day RSI, otherwise, plot a 14-day RSI. The next formula plots “positive volume” if the close is greater than the median price. Otherwise, "negative volume" is plotted.

iif( CLOSE > (HIGH+LOW)/2, Volume, -Volume ); 

If you simply want an expression to be evaluated as either true or false, it can be done without the use of the if() function. The following formula will result in either a 1 (true) or a 0 (false):

rsi(14) > 70; 

The same done with iif() gives the same results, but the formula is longer.

iif(rsi(14) > 70, 1, 0 ); 

Logical operators

If a formula requires multiple conditions, you can combine the conditions with AND and OR operators. For example, maybe you'd like to plot a +1 when the MACD is greater than zero and the RSI is greater than 70:

macd() > 0 AND rsi(14) > 70; 

You can add as many conditions within a formula as you like.

You can even combine AND and OR operators within the same formula as follows:

( macd() > 0 OR close > mov(close,10,e) ) AND rsi(14) > 70; 

Variables

In order to shorten, simplify, enhance, and make the maintenance of complex formulas easier, you may want to use variables. In fact using variables you can significantly improve formula calculation speed. So it is strongly recommended to use variables.

A variable is an alphanumeric name that is assigned to an expression or a single value. Unlimited number of variables can be used in a formula. Variables must be assigned before the variable is used in the formula. Variables cannot be assigned within a function call.

AmiBroker uses some reserved variable names in its formulas, for example in Auto-Analysis window you have to assign values to 2 variables named 'buy' or 'sell' to specify the conditions where "buy" and "sell" conditions occur. For example (system that buys when MACD rises above 0 line, and sells when MACD falls below 0 line)

buy  = cross( macd(), 0 );
sell = cross( 0, macd() ); 

The following rules apply to the naming of variables:

Function Reference (AFL 1.0)

  1. ABS
    SYNTAX abs( DATA ARRAY )
    FUNCTION Calculates the absolute value of the DATA ARRAY.
    EXAMPLE The formula "abs( -10 )" will return +10; the formula "abs( 10 )" also returns +10.
  2. ACCDIST
    SYNTAX accdist()
    FUNCTION Calculates the predefined Accumulation/ Distribution indicator.
    EXAMPLE
  3. ATAN
    SYNTAX atan( DATA ARRAY )
    FUNCTION Returns the arc tangent of DATA ARRAY. The value is returned in radians
    EXAMPLE The formula "atan( 10 0 )" returns PI/2
    SEE ALSO The cos() function (see Cosine) ; the sin() function (see Sine)
  4. BARSSINCE
    SYNTAX barssince( DATA ARRAY )
    FUNCTION Calculates the number of bars (time periods) that have passed since DATA ARRAY was true.
    EXAMPLE barssince( macd() < 0 )
  5. BBANDBOT
    SYNTAX bbandbot( DATA ARRAY, PERIODS, DEVIATIONS )
    FUNCTION Calculates the bottom Bollinger Band of DATA ARRAY shifted downward DEVIATION standard deviations.
    EXAMPLE bbandbot( close, 10, 2 )
    SEE ALSO  
  6. BBANDTOP
    SYNTAX bbandtop( DATA ARRAY, PERIODS, DEVIATIONS )
    FUNCTION Calculates the bottom Bollinger Band of DATA ARRAY shifted upward DEVIATION standard deviations.
    EXAMPLE bbandtop( close, 10, 2 )
    SEE ALSO  
  7. CEIL
    SYNTAX ceil( DATA ARRAY )
    FUNCTION Calculates the lowest integer that is greater than DATA ARRAY.
    EXAMPLE The formula "ceiling( 7.2 )" returns 8; the formula "ceiling(-7.2)" returns -7.
    SEE ALSO The floor() function (see Floor); the int() function (see Integer).
  8. CHAIKIN
    SYNTAX chaikin( PERIODS )
    FUNCTION Calculates the predefined Chaikin Oscillator.
    EXAMPLE  
    SEE ALSO  
  9. CCI
    SYNTAX cci( PERIODS )
    FUNCTION Calculates the predefined Commodity Channel Index.
    EXAMPLE cci( 14 )
    SEE ALSO  
  10. COS
    SYNTAX cos( DATA ARRAY )
    FUNCTION Returns the cosine of DATA ARRAY. Assumes that the DATA ARRAY values are in radians.
    EXAMPLE cos( C )
    SEE ALSO The atan() function (see Arc Tangent); the sin() function (see Sine).
  11. CROSS
    SYNTAX cross( DATA ARRAY 1, DATA ARRAY 2 )
    FUNCTION Plots a "+1" on the day that DATA ARRAY 1 crosses above DATA ARRAY 2. Otherwise, "0" is plotted.
    If you want to know when DATA ARRAY 1 crosses below DATA ARRAY 2, use the formula "cross( DATA ARRAY 2, DATA ARRAY 1)"
    EXAMPLE cross( close, mov(close,9,e) )
    SEE ALSO  
  12. CUM
    SYNTAX cum( DATA ARRAY )
    FUNCTION Calculates a cumulative sum of the DATA ARRAY from the first period in the chart.
    EXAMPLE The formula "cum( 1 )" calculates an indicator that rises one point for each day since the beginning of the chart; the formula "cum( C )" calculates the cumulative total of all closing prices from the beginning of the chart.
    SEE ALSO The sum() function (see Summation).
  13. EMA
    SYNTAX ema( DATA ARRAY, PERIODS)
    FUNCTION Calculates a PERIODS exponential moving average of DATA ARRAY
    EXAMPLE ema(CLOSE, 5 )
    SEE ALSO ma() Simple Moving Average function
  14. EXP
    SYNTAX exp( DATA ARRAY )
    FUNCTION Calculates e raised to the DATA ARRAY power.
    EXAMPLE  
    SEE ALSO The log() function (see Logarithm (natural)).
  15. FLOOR
    SYNTAX floor( DATA ARRAY )
    FUNCTION Calculates the highest integer that is less than DATA ARRAY.
    EXAMPLE The function "floor( 17.9 )" returns 17. The formula "floor( -17.9 )" returns -18.
    SEE ALSO The ceiling() function (see Ceiling); the int() function (see Integer).
  16. FRAC
    SYNTAX frac( DATA ARRAY )
    FUNCTION Eliminates the integer portion of DATA ARRAY and returns the fractional part.
    EXAMPLE The formula "frac( 10.7 )" returns 0.7; the formula "frac(-19.8 )" returns -0.8.
    SEE ALSO The int() function (see Integer).
  17. GAPDOWN
    SYNTAX gapdown()
    FUNCTION Plots a "+1" on the day a security's prices gap down. Otherwise a "0" is plotted. A gap down occurs if yesterday's low is greater than today's high.
    EXAMPLE  
    SEE ALSO  
  18. GAPUP
    SYNTAX gapup()
    FUNCTION Plots a "+1" on the day a security's prices gap up. Otherwise a "0" is plotted. A gap up occurs if yesterday's high is less than today's low.
    EXAMPLE  
    SEE ALSO  
  19. HIGHEST
    SYNTAX highest( DATA ARRAY )
    FUNCTION Calculates the highest value in the DATA ARRAY since the first day loaded in the chart.
    EXAMPLE The formula "highest( rsi(14) )" returns the highest Relative Strength Index value since the first day loaded in the chart; "highest ( close )" returns the highest closing price since the first day loaded in the chart.
    SEE ALSO The hhv() function Highest High Value ; the llv() function (see Lowest Low Value); the lowest() function (see Lowest).
  20. HIGHESTBARS
    SYNTAX highestbars( DATA ARRAY )
    FUNCTION Calculates the number of periods that have passed since the DATA ARRAY’s highest value. This includes all data loaded in the chart.
    EXAMPLE The formula "highestbars( close )" returns the number of periods that have passed since the closing price reached its highest peak.
    SEE ALSO  
  21. HHV
    SYNTAX hhv( DATA ARRAY, PERIODS )
    FUNCTION Calculates the highest value in the DATA ARRAY over the preceding PERIODS (PERIODS includes the current day).
    EXAMPLE The formula "hhv( CLOSE, 5 )" returns the highest closing price over the preceding five periods; "hhv(H,7)" returns the highest high price over the preceding seven periods.
    SEE ALSO the llv() function (see Lowest Low Value).
  22. HHVBARS
    SYNTAX hhvbars( DATA ARRAY, PERIODS )
    FUNCTION Calculates the number of periods that have passed since the DATA ARRAY reached its PERIODS period peak.
    EXAMPLE The formula "hhvbars( close,50 )" returns the number of periods that have passed since the closing price reached its 50-period peak.
    SEE ALSO  
  23. HOLD
    SYNTAX hold( EXPRESSION, PERIODS )
    FUNCTION Extends a "true" result of EXPRESSION for the specified number of periods. This true result is held true over the number of periods specified even if a "false" result is generated.
    EXAMPLE hold( cross(rsi(14),70),5 )
  24. IIF
    SYNTAX iif( CONDITION DATA ARRAY, THEN DATA ARRAY, ELSE DATA ARRAY )
    FUNCTION A conditional function that returns the second parameter (THEN) if the conditional expression defined by the first parameter is true; otherwise, the third parameter is returned (ELSE).
    EXAMPLE The formula "if(1<2,3,4)" will always return the value three.
    SEE ALSO For more details on using the iif() function, see The iif() function.
  25. INSIDE
    SYNTAX inside()
    FUNCTION Plots a "+1" when an inside day occurs. An inside day occurs when today's high is less than yesterday's high and today's low is greater than yesterday's low.
    EXAMPLE  
    SEE ALSO  
  26. INT
    SYNTAX int( DATA ARRAY )
    FUNCTION Removes the fractional portion of DATA ARRAY and returns the integer part.
    EXAMPLE The formula "int( 10.7 )" returns 10; the formula "int(-19.8 )" returns -19.
    SEE ALSO The ceiling() function (see Ceiling); the floor() function (see Floor); the frac() function (see Fraction).
  27. LASTVALUE
    SYNTAX lastvalue(DATA ARRAY)
    FUNCTION Returns last calculated value of the specified DATA ARRAY. The result of this function can be used in place of a constant in any function argument.
    If DATA ARRAY is undefined (e.g., only 100-days loaded and you request the last value of a 200-day moving average) then the lastvalue function returns zero.
    Since this function loads an entire data array with the last value of another array, it allows a formula to look into the future. This is unacceptable for most indicators, but is very beneficial for things like pattern recognition.
    EXAMPLE  
    SEE ALSO  
  28. LOG
    SYNTAX log( DATA ARRAY )
    FUNCTION Calculates the natural logarithm of DATA ARRAY.
    EXAMPLE  
    SEE ALSO exp() Exponential function
  29. LOG10
    SYNTAX log10( DATA ARRAY )
    FUNCTION Calculates the decimal logarithm of DATA ARRAY.
    EXAMPLE  
    SEE ALSO .
  30. LOWEST
    SYNTAX lowest( DATA ARRAY )
    FUNCTION Calculates the lowest value in the DATA ARRAY since the first day loaded in the chart (PERIODS includes the current day).
    EXAMPLE The formula "lowest( rsi(14) )" returns the lowest Relative Strength Index value since the first day loaded in the chart; "lowest ( close )" returns the lowest closing price since the first day loaded in the chart.
    SEE ALSO The hhv() function (see Highest High Value ); the llv() function (see Lowest Low Value); the highest() function (see Highest).
  31. LOWESTBARS
    SYNTAX lowestbars( DATA ARRAY )
    FUNCTION Calculates the number of periods that have passed since the DATA ARRAY’s lowest value. This includes all data loaded in the chart.
    EXAMPLE The formula"lowestbars( close )" returns the number of periods that have passed since the closing price reached its lowest point.
    SEE ALSO  
  32. LLV
    SYNTAX llv( DATA ARRAY, PERIODS )
    FUNCTION Calculates the lowest value in the DATA ARRAY over the preceding PERIODS (PERIODS includes the current day).
    EXAMPLE The formula "llv( CLOSE, 14 )" returns the lowest closing price over the preceding 14 periods.
    SEE ALSO The hhv() function (see Highest High Value ).
  33. LLVBARS
    SYNTAX llvbars( DATA ARRAY, PERIODS )
    FUNCTION Calculates the number of periods that have passed since the DATA ARRAY reached its PERIODS period trough.
    EXAMPLE The formula "llvbars( close,50 )" returns the number of periods that have passed since the closing price reached its 50 period trough.
    SEE ALSO  
  34. MA
    SYNTAX SYNTAX ma( DATA ARRAY, PERIODS)
    FUNCTION Calculates a PERIODS simple moving average of DATA ARRAY
    EXAMPLE ma(CLOSE, 5 )
    SEE ALSO ema() Exponential Moving Average function
  35. MACD
    SYNTAX macd(PERIOD1,PERIOD2)
    FUNCTION Calculates the predefined MACD indicator.
    EXAMPLE The formula "macd()" returns the value of the MACD indicator (i.e., the solid line). The formula "signal()" returns the value of the MACD's signal line (i.e., the dotted line).
    SEE ALSO The signal() function.
  36. MAX
    SYNTAX max( DATA ARRAY, DATA ARRAY )
    FUNCTION Returns the largest of the two parameters.
    EXAMPLE The formula "max( CLOSE, 10 )" returns either the closing price or 10, whichever is greater. The formula "max(-14, 13)" always returns 13.
    SEE ALSO  
  37. MIN
    SYNTAX min( DATA ARRAY, DATA ARRAY )
    FUNCTION Returns the smallest of the two parameters.
    EXAMPLE The formula "min( CLOSE, 10 )" returns the closing price or 10, whichever is less. The formula "min(-14, 13)" always returns -14.
    SEE ALSO The max() function (see Maximum).
  38. MFI
    SYNTAX mfi( PERIODS )
    FUNCTION Calculates the predefined Money Flow Index.
    EXAMPLE mfi( 14 )
    SEE ALSO The rsi() function (see Relative Strength Index (RSI)).
  39. NVI
    SYNTAX nvi()
    FUNCTION Calculates the predefined Negative Volume Index.
    EXAMPLE  
    SEE ALSO The pvi() function (see Positive Volume Index).
  40. OBV
    SYNTAX obv()
    FUNCTION Calculates the predefined On Balance Volume indicator.
    EXAMPLE  
    SEE ALSO  
  41. PVI
    SYNTAX pvi()
    FUNCTION Calculates the predefined Positive Volume Index.
    EXAMPLE  
    SEE ALSO The nvi() function (see Negative Volume Index).
  42. PREC
    SYNTAX prec( DATA ARRAY, PRECISION )
    FUNCTION Truncates DATA ARRAY to PRECISION decimal places.
    EXAMPLE The formula "prec( 10.12981, 2 )" returns 10.120. The formula "prec( 10.12981, 4 )" returns 10.12980. Small binary rounding errors may cause some minor distortion in the decimal portion of any number stored in a computer.
    SEE ALSO  
  43. ROC
    SYNTAX roc( DATA ARRAY, PERIODS)
    FUNCTION Calculates the PERIODS rate-of-change of DATA ARRAY expressed as percentage.
    EXAMPLE The formula "roc( CLOSE, 12, PERCENT )" returns the 12-period percent rate-of-change of the closing prices.
    SEE ALSO  
  44. REF
    SYNTAX ref( DATA ARRAY, PERIODS )
    FUNCTION References a previous or subsequent element in a DATA ARRAY. A positive PERIOD references "n" periods in the future; a negative PERIOD references "n" periods ago.
    EXAMPLE The formula "ref( CLOSE, -12 )" returns the closing price 12 periods ago. Thus, you could write the 12-day price rate-of-change (expressed in points) as "C - ref( C, -12 )." The formula "ref( C, +12 )" returns the closing price 12 periods ahead.
    SEE ALSO  
  45. RSI
    SYNTAX rsi( PERIODS )
    FUNCTION Calculates the predefined RSI indicator.
    EXAMPLE rsi( 14 )
    SEE ALSO  
  46. ROUND
    SYNTAX round( DATA ARRAY )
    FUNCTION Rounds DATA ARRAY to the nearest integer.
    EXAMPLE The formula "round( +10.5 )" returns +11. The formula "round( -10.4 )" returns -10.
    SEE ALSO The ceiling() function Ceiling; the floor() function Floor; the int() function Integer.
  47. SIGNAL
    SYNTAX signal(PERIOD1,PERIOD2,SIGNAL PERIOD)
    FUNCTION Calculates the predefined Signal line of MACD indicator.
    EXAMPLE  
    SEE ALSO  
  48. SIN
    SYNTAX sin( DATA ARRAY )
    FUNCTION Returns the sine of DATA ARRAY. This function assumes that the DATA ARRAY values are in radians.
    EXAMPLE You can plot a sine wave using the formula "sin(cum(0.1))." Increasing the value in this formula (i.e., "0.1") will increase the frequency of the sine wave.
    SEE ALSO The atan() function (see Arc Tangent); the cos() function (see Cosine).
  49. SQRT
    SYNTAX sqrt( DATA ARRAY )
    FUNCTION Calculates the square root of DATA ARRAY. The square root of a negative number always returns a zero result.
    EXAMPLE The formula "sqrt( 16 )" returns 4
    SEE ALSO  
  50. STOCHK
    SYNTAX stochk( %K PERIODS )
    FUNCTION Calculates the predefined %K line of Stochastic Oscillator (with internal slowing 3).
    EXAMPLE The formula "stochk( 5 )" returns the value of a 5-period %K slowed down 3 periods.
    SEE ALSO  
  51. STOCHD
    SYNTAX stochd( %K PERIODS )
    FUNCTION Calculates the predefined %D line of Stochastic Oscillator (with internal slowing 3).
    EXAMPLE The formula "stochd( 5 )" returns the value of a 5-period %D slowed down 3 periods.
  52. SUM
    SYNTAX sum( DATA ARRAY, PERIODS )
    FUNCTION Calculates a cumulative sum of the DATA ARRAY for the specified number of lookback PERIODs (including today).
    EXAMPLE The formula "sum( CLOSE, 12 )" returns the sum of the preceding 12 closing prices. A 12-period simple moving average could be written "sum(C,12) / 12."
    SEE ALSO The cum() function (see Cumulate).
  53. TRIX
    SYNTAX trix( PERIODS )
    FUNCTION Calculates the predefined TRIX indicator.
    EXAMPLE trix( 12 )
    SEE ALSO  
  54. ULTIMATE
    SYNTAX ultimate( CYCLE1, CYCLE2, CYCLE3 )
    FUNCTION Calculates the predefined Ultimate Oscillator indicator using the three cycle lengths supplied as parameters. Note that each of the three parameters must be greater than the preceding parameter or an error message will be displayed (e.g., "ultimate( 5, 5, 5)" is not valid).
    EXAMPLE The formula "ultimate( 7, 14, 21 )" returns the default Ultimate Oscillator.
    SEE ALSO  
  55. WRITEIF
    SYNTAX writeif( EXPRESSION, "TRUE TEXT", "FALSE TEXT" )
    FUNCTION This function can only be used within an Guru commentary. If EXPRESSION evaluates to "true", then the TRUE TEXT string is displayed within the commentary. If EXPRESSION evaluates to "false", then the FALSE TEXT string is displayed.
    EXAMPLE writeif( c > mov(c,200,s), "The close is above the 200-period moving average.","The close is below the 200-period moving average." )
    SEE ALSO writeval() function
  56. WRITEVAL
    SYNTAX writeval( DATA ARRAY )
    FUNCTION This function can only be used within an Guru commentary. It is used to display the numeric value of DATA ARRAY.
    EXAMPLE writeval( stoch(39) - stoch(12) )
    SEE ALSO writeif() function

Function Reference (functions avaliable in AFL 1.1 and above)

  1. NAME
    SYNTAX name()
    FUNCTION This function can only be used within an Guru commentary. It is used to display the stock short name (ticker)
    EXAMPLE name()
  2. FULLNAME
    SYNTAX fullname()
    FUNCTION This function can only be used within an Guru commentary. It is used to display the stock full name
    EXAMPLE fullname()
  3. DATE
    SYNTAX date()
    FUNCTION This function can only be used within an Guru commentary. It is used to display the selected date
    EXAMPLE date()
  4. OSCP
    SYNTAX oscp(fast periods,slow periods)
    FUNCTION Calculates price oscillator based on exponential moving averages
    EXAMPLE oscp()
  5. OSCV
    SYNTAX oscv(fast periods,slow periods )
    FUNCTION Calculates volume oscillator based on exponential moving averages
    EXAMPLE oscv()
  6. ZIG
    SYNTAX zig(DATA ARRAY, %MINIMUM CHANGE)
    FUNCTION Calculates the %MINIMUM CHANGE predefined Zig Zag indicator
    EXAMPLE zig(close,5)
  7. PEAK
    SYNTAX peak(DATA ARRAY, %MINIMUM CHANGE, Nth)
    FUNCTION Plots the value of DATA ARRAY Nth peak(s) ago. This uses the Zig Zag function (see Zig Zag) to determine the peaks. N=1 would return the value of the most recent peak. N=2 would return the value of the 2nd most recent peak.
    EXAMPLE peak(close,5,1)
  8. PEAKBARS
    SYNTAX peakbars(DATA ARRAY, %MINIMUM CHANGE, Nth)
    FUNCTION Plots the number of bars that have passed from the Nth peak. This uses the Zig Zag function (see Zig Zag) to determine the peaks. N=1 would return the number of bars that have passed since the most recent peak. N=2 would return the number of bars that have passed since the 2nd most recent peak
    EXAMPLE peakbars(close,5,1)
  9. TROUGH
    SYNTAX trough(DATA ARRAY, %MINIMUM CHANGE, Nth)
    FUNCTION Plots the value of DATA ARRAY Nth trough(s) ago. This uses the Zig Zag function (see Zig Zag) to determine the troughs. N=1 would return the value of the most recent trough. N=2 would return the value of the 2nd most recent trough.
    EXAMPLE trough(close,5,1)
  10. TROUGHBARS
    SYNTAX troughbars(DATA ARRAY, %MINIMUM CHANGE, Nth)
    FUNCTION Plots the number of bars that have passed from the Nth trough. This uses the Zig Zag function (see Zig Zag) to determine the troughs. N=1 would return the number of bars that have passed since the most recent trough. N=2 would return the number of bars that have passed since the 2nd most recent trough
    EXAMPLE troughbars(close,5,1)
  11. VALUEWHEN
    SYNTAX valuewhen(EXPRESSION, DATA ARRAY, Nth)
    FUNCTION Returns the value of the DATA ARRAY when the EXPRESSION was true on the Nth most recent occurrence. This includes all data loaded in the chart.
    EXAMPLE valuewhen( cross( close, ma(close,5) ) ,macd(), 1)